package com.collection.service.impl;

import java.util.Date;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.collection.dao.SysAdminDao;
import com.collection.dto.FilterDto;
import com.collection.entity.SysAdmin;
import com.collection.service.SysAdminService;
import com.collection.util.EncryptUtil;
import com.collection.util.HQLHelpUtil;
import com.collection.util.Page;


/**
 * 管理员管理服务实现类
 * @author 冯勇
 *
 */
@Service
public class SysAdminServiceImpl implements SysAdminService {

	@Autowired
	private SysAdminDao sysAdminDao;
	
	/*
	 * (non-Javadoc)
	 * @see net.wlsj.service.SysAdminService#createSysAdmin(net.wlsj.entity.SysAdmin)
	 */
	@Override
	public void createSysAdmin(SysAdmin sysAdmin) {
		if(sysAdmin != null) {
			sysAdminDao.save(sysAdmin);
		} 
	}

	/*
	 * (non-Javadoc)
	 * @see net.wlsj.service.SysAdminService#deleteSysAdmin(java.lang.Integer)
	 */
	@Override
	public void deleteSysAdmin(Integer id) {
		if(id != null) {
			SysAdmin admin = getSysAdminById(id);
			if(admin != null) {
				sysAdminDao.delete(admin);
			}
		}
	}

	/*
	 * (non-Javadoc)
	 * @see net.wlsj.service.SysAdminService#deleteSysAdmin(net.wlsj.entity.SysAdmin)
	 */
	@Override
	public void deleteSysAdmin(SysAdmin sysAdmin) {
		if(sysAdmin != null) {
			sysAdminDao.delete(sysAdmin);
		}
	}

	/*
	 * (non-Javadoc)
	 * @see net.wlsj.service.SysAdminService#findAllSysAdmins()
	 */
	@Override
	public List<SysAdmin> findAllSysAdmins() {
		return sysAdminDao.queryByHQL("from SysAdmin ");
	}

	/*
	 * (non-Javadoc)
	 * @see net.wlsj.service.SysAdminService#findSysAdmins(net.wlsj.util.Page)
	 */
	@Override
	public void findSysAdmins(Page<SysAdmin> page) {
		String countHql = "select count(*) from SysAdmin ";
		String hql = " from SysAdmin ";
		List<Object> filedValues = null;
		if(page != null) {
			//查询过滤条件
			Map<FilterDto, Object> filter = page.getFilter();
			if(filter != null) {
				String filterHql = HQLHelpUtil.createHQL(filter);
				countHql += " where " + filterHql;
				hql += " where " + filterHql;
				filedValues =  HQLHelpUtil.getFiledValues();
			}
			if(filedValues != null && filedValues.size() > 0) {
				page.setTotalCount(sysAdminDao.getRecordCount(countHql, filedValues.toArray()));
				page.setResult(sysAdminDao.queryByHQL(hql, filedValues.toArray(), page.getStart(), page.getPageSize(), page.getOrder()));
			} else {
				page.setTotalCount(sysAdminDao.getRecordCount(countHql));
				page.setResult(sysAdminDao.queryByHQL(hql, page.getStart(), page.getPageSize(), page.getOrder()));
			}
		}
	}

	/*
	 * (non-Javadoc)
	 * @see net.wlsj.service.SysAdminService#getSysAdminById(java.lang.Integer)
	 */
	@Override
	public SysAdmin getSysAdminById(Integer id) {
		if(id != null) {
			return sysAdminDao.get(SysAdmin.class, id);
		}
		return null;
	}

	/*
	 * (non-Javadoc)
	 * @see net.wlsj.service.SysAdminService#modifySysAdmin(net.wlsj.entity.SysAdmin)
	 */
	@Override
	public void modifySysAdmin(SysAdmin sysAdmin) {
		if(sysAdmin != null) {
			sysAdmin.setUpdateDate(new Date());
			sysAdminDao.update(sysAdmin);
		}
	}

	/*
	 * (non-Javadoc)
	 * @see net.wlsj.service.SysAdminService#login(net.wlsj.entity.SysAdmin)
	 */
	@Override
	public boolean login(SysAdmin admin) {
		
		if(admin != null) {
			String userName = admin.getUserName();
			String password = admin.getPassword();
			
			//通过管理员账号与密码获取管理员对象
			List<SysAdmin> loginAdmin = sysAdminDao.queryByHQL("from SysAdmin a where a.userName=? and a.password=?", new Object[]{userName, EncryptUtil.md5(password)});
			
			if(loginAdmin != null && loginAdmin.size() > 0) {
				return true;
			} 
		}
		return false;
	}

}
